//
//  YFTimePicker.m
//  DatePickerDemo
//
//  Created by Winny Zhang on 2019/4/4.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFTimePicker.h"
#import "NSDate+YFExtension.h"

@interface YFTimePicker () <UIPickerViewDataSource, UIPickerViewDelegate>

/**
 时钟选择器 PickerView
 */
@property (weak, nonatomic) IBOutlet UIPickerView *hourPicker;
/**
 分钟选择器 PickerView
 */
@property (weak, nonatomic) IBOutlet UIPickerView *minutePicker;

/**
 时钟 数组
 */
@property (nonatomic, strong) NSArray <NSString *> *hours;

/**
 分钟 数组
 */
@property (nonatomic, strong) NSArray <NSString *> *minutes;

/**
 选中的时钟 默认当前时间的年份
 */
@property (nonatomic,assign) NSInteger selectedHour;

/**
 选中的分钟 默认当前时间的月份
 */
@property (nonatomic, assign) NSInteger selectedMinute;


@end

@implementation YFTimePicker

/**
 初始化
 
 @param frame frame
 @return timePicker
 */
+ (instancetype)timePickerWithFrame:(CGRect)frame {
    YFTimePicker *picker = [[NSBundle mainBundle] loadNibNamed:@"YFTimePicker" owner:nil options:nil].firstObject;
    picker.frame = frame;
    return picker;
}

/**
 init方法初始化
 
 @param frame frame
 @return datePicker
 */
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self = [[NSBundle mainBundle] loadNibNamed:@"YFTimePicker" owner:nil options:nil].firstObject;
        [self setupViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupViews];
    }
    return  self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupViews];
}

- (void)setupViews {
    
    self.hourPicker.dataSource = self;
    self.hourPicker.delegate = self;
    self.minutePicker.dataSource = self;
    self.minutePicker.delegate = self;
    
    self.selectedHour = [NSDate date].currentHour;
    self.selectedMinute = [NSDate date].currentMinute;
    
    [self.hourPicker reloadAllComponents];
    [self.minutePicker reloadAllComponents];
    
    NSString *selectHourString = [NSString stringWithFormat:@"%zd", self.selectedHour];
    [self.hourPicker selectRow: [self indexInArray:self.hours value:selectHourString] inComponent:0 animated:YES];
    
    NSString *selectedMinuteString = [NSString stringWithFormat:@"%zd", self.selectedMinute];
    [self.minutePicker selectRow:[self indexInArray:self.minutes value:selectedMinuteString] inComponent:0 animated:YES];
}


#pragma mark - UIPickerViewDataSource, UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    switch (pickerView.tag) {
        case 3600:
            return self.hours.count;
            break;
        case 60:
            return self.minutes.count;
            break;
        default:
            return 0;
            break;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    UIColor *lineColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
    
    // 隐藏横线
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = lineColor;
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = lineColor;
    
    // 显示内容
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, pickerView.frame.size.width, 35);
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    // 赋值
    switch (pickerView.tag) {
        case 3600:
            label.text = self.hours[row];
            break;
        case 60:
            label.text = self.minutes[row];
            break;
        default:
            break;
    }
    return label;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 35;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    switch (pickerView.tag) {
        case 3600:
            self.selectedHour = self.hours[row].integerValue;
            break;
        case 60:
            self.selectedMinute = self.minutes[row].integerValue;
            break;
        default:
            break;
    }
    if (_selectedTimeCompletion) {
        
        NSString *hourString = [NSString stringWithFormat:@"%zd", self.selectedHour];
        if (self.selectedHour < 10) {
            hourString = [NSString stringWithFormat:@"0%zd", self.selectedHour];
        }
        
        NSString *minuteString = [NSString stringWithFormat:@"%zd", self.selectedMinute];
        if (self.selectedMinute < 10) {
            minuteString = [NSString stringWithFormat:@"0%zd", self.selectedMinute];
        }

        _selectedTimeCompletion(hourString, minuteString);
    }
}


#pragma mark - 获取元素在数组中的位置
- (NSInteger)indexInArray:(NSArray <NSString *> *)array value:(NSString *)value {
    __block NSInteger index = 0;
    [array enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([value isEqualToString:obj]) {
            index = idx;
        }
    }];
    return index;
}


#pragma mark - lazy Load
- (NSArray <NSString *> *)hours {
    if (!_hours) {
        NSMutableArray *hours = [NSMutableArray arrayWithCapacity:24];
        NSInteger hour = 0;
        for (int i = 0; i < 24; i++) {
            if (hour < 10) {
                [hours addObject:[NSString stringWithFormat:@"0%zd", hour]];
            } else {
                [hours addObject:[NSString stringWithFormat:@"%zd", hour]];
            }
            hour += 1;
        }
        _hours = hours;
    }
    return _hours;
}

- (NSArray <NSString *> *)minutes {
    if (!_minutes) {
        NSMutableArray *minutes = [NSMutableArray arrayWithCapacity:60];
        NSInteger minute = 0;
        for (int i = 0; i < 60; i++) {
            if (minute < 10) {
                [minutes addObject:[NSString stringWithFormat:@"0%zd", minute]];
            } else {
                [minutes addObject:[NSString stringWithFormat:@"%zd", minute]];
            }
            minute += 1;
        }
        _minutes = minutes;
    }
    return _minutes;
}

@end
